home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1998 June / Cd Pc Users 9.iso / prog / inst / baslibs / bassound.bas < prev    next >
Encoding:
BASIC Source File  |  1996-12-30  |  5.7 KB  |  212 lines

  1. Attribute VB_Name = "basSound"
  2. Option Explicit
  3.  
  4. Private Const SND_ALIAS = &H10000
  5. Private Const SND_ASYNC = &H1
  6. Private Const SND_FILENAME = &H20000
  7. Private Const SND_LOOP = &H8
  8. Private Const SND_NODEFAULT = &H2
  9. Private Const SND_NOSTOP = &H10
  10. Private Const SND_NOWAIT = &H2000
  11. Private Const SND_SYNC = &H0
  12.  
  13. Private Declare Function PlaySound Lib "winmm.dll" Alias _
  14.    "PlaySoundA" (ByVal lpszName As String, _
  15.    ByVal hModule As Long, ByVal dwFlags As Long) As Long
  16.  
  17. Private Declare Function waveOutGetNumDevs Lib "winmm" () As Long
  18. Private Declare Function midiOutGetNumDevs Lib "winmm" () As Integer
  19.  
  20. Private Declare Function midiOutGetVolume Lib "winmm.dll" _
  21.    (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
  22. Private Declare Function waveOutGetVolume Lib "winmm.dll" _
  23.    (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
  24.    
  25. Public Declare Function midiOutSetVolume Lib "winmm.dll" _
  26.    (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
  27. Public Declare Function waveOutSetVolume Lib "winmm.dll" _
  28.    (ByVal uDeviceID As Long, lpdwVolume As Long) As Long
  29.  
  30. Private Declare Function mciSendString Lib "winmm.dll" Alias _
  31.    "mciSendStringA" (ByVal lpstrCommand As String, _
  32.    ByVal lpstrReturnString As String, ByVal uReturnLength As Long, _
  33.    ByVal hwndCallback As Long) As Long
  34.    
  35.    
  36. Private Const MMSYSERR_NOERROR = 0
  37.  
  38.  
  39.  
  40. Public Const AUDIO_NONE = 0
  41. Public Const AUDIO_WAVE = 1
  42. Public Const AUDIO_MIDI = 2
  43.  
  44. '
  45. ' Returns 1 if wave output
  46. ' Returns 2 if midi output
  47. ' Returns 3 if both
  48. '
  49. Public Function CanPlaySound() As Integer
  50.    Dim i As Integer
  51.  
  52.    i = AUDIO_NONE
  53.    
  54.    If waveOutGetNumDevs > 0 Then
  55.       i = AUDIO_WAVE
  56.    End If
  57.    
  58.    If midiOutGetNumDevs > 0 Then
  59.       i = i + AUDIO_MIDI
  60.    End If
  61.    
  62.    CanPlaySound = i
  63. End Function
  64.  
  65. '
  66. ' Bug: Does not work correctly
  67. Public Function GetVolume(Optional rt As Variant, Optional lt As Variant, Optional audiotype As Variant) As Integer
  68.    Dim i As Long
  69.    Dim k As Integer
  70.    
  71.    rt = 0
  72.    lt = 0
  73.    k = 0
  74.    
  75.    If IsMissing(audiotype) Then
  76.       audiotype = AUDIO_MIDI + AUDIO_WAVE
  77.    End If
  78.    
  79.    If (audiotype And AUDIO_MIDI) = AUDIO_MIDI Then
  80.       midiOutGetVolume 0, i
  81.       rt = ((i And &HFFFF0000) \ &HFFFF&) And &HFFFF&
  82.       lt = i And &HFFFF&
  83.       k = 1
  84.    End If
  85.    
  86.    If (audiotype And AUDIO_WAVE) = AUDIO_WAVE Then
  87.       waveOutGetVolume 0, i
  88.       rt = rt + ((i And &HFFFF0000) / &H10000) And &HFFFF&
  89.       lt = lt + (i And &HFFFF&)
  90.       k = k + 1
  91.    End If
  92.  
  93.    If k = 0 Then
  94.       GetVolume = 0
  95.    Else
  96.       GetVolume = (rt + lt) / (k * 2)
  97.       rt = rt / k
  98.       lt = lt / k
  99.    End If
  100. End Function
  101.  
  102.  
  103. '
  104. '
  105. ' Bug: Does not work correctly
  106. Public Sub SetVolume(ByVal rt As Integer, ByVal lt As Integer, Optional audiotype As Variant)
  107.    If IsMissing(audiotype) Then
  108.       audiotype = AUDIO_MIDI + AUDIO_WAVE
  109.    End If
  110.    
  111.    If (audiotype And AUDIO_MIDI) = AUDIO_MIDI Then
  112.       midiOutSetVolume 0, (rt * &HFFFF&) + lt
  113.    End If
  114.    
  115.    If (audiotype And AUDIO_WAVE) = AUDIO_WAVE Then
  116.       waveOutSetVolume 0, (rt * &HFFFF&) + lt
  117.    End If
  118. End Sub
  119.  
  120.  
  121. '
  122. ' Typical system sounds constant across all windows platforms
  123. '
  124. '    SystemQuestion
  125. '    SystemStart
  126. '    SystemAsterisk
  127. '    SystemExclamation
  128. '    SystemExit
  129. '    SystemHand
  130. '
  131. '  Returns true if success, false if failed.
  132. '  async assumes true
  133. '  loop assumes false
  134. Public Function SoundPlay(filename As String, Optional async As Variant, Optional sLoop As Variant) As Boolean
  135.    Dim i As Integer
  136.    Dim f As String
  137.    Dim j As Long
  138.          
  139.    i = Len(filename)
  140.    f = UCase(filename)
  141.    
  142.    If IsMissing(async) Then
  143.       j = SND_ASYNC
  144.    Else
  145.       If async Then
  146.          j = SND_ASYNC
  147.       Else
  148.          j = SND_SYNC
  149.       End If
  150.    End If
  151.    
  152.    If Not IsMissing(sLoop) Then
  153.       If sLoop And (j = SND_ASYNC) Then
  154.          j = j + SND_LOOP
  155.       End If
  156.    End If
  157.    
  158.    j = j + SND_NOSTOP + SND_NOWAIT
  159.    
  160.    If InStr(f, ".WAV") = i - 3 Then
  161.       If CanPlaySound And AUDIO_WAVE = AUDIO_WAVE Then
  162.          j = j + SND_FILENAME + SND_NODEFAULT
  163.          i = PlaySound(filename, 0, j)
  164.          SoundPlay = IIf(i = 0, False, True)
  165.       Else
  166.          Beep
  167.          SoundPlay = True
  168.       End If
  169.       
  170.    'Assume media player for other file names   .MID .RMI etc..
  171.    ElseIf InStr(f, ".") = i - 3 Then
  172.       If CanPlaySound And AUDIO_MIDI = AUDIO_MIDI Then
  173.          i = mciSendString("open " & filename & " type sequencer alias filename", 0&, 0, 0)
  174.          'Note the true/false order is supposed to be opposite of the others.
  175.          SoundPlay = IIf(i = 0, True, False)
  176.          If (j And SND_ASYNC) = SND_ASYNC Then
  177.             If (j And SND_LOOP) = SND_LOOP Then
  178.                'Bug: repeat doesn't work.
  179.                mciSendString "play filename repeat", 0&, 0, 0
  180.             Else
  181.                mciSendString "play filename", 0&, 0, 0
  182.             End If
  183.          Else
  184.             mciSendString "play filename wait", 0&, 0, 0
  185.             mciSendString "close filename", 0&, 0, 0
  186.          End If
  187.       Else
  188.          Beep
  189.          SoundPlay = True
  190.       End If
  191.    Else
  192.       j = j + SND_ALIAS
  193.       i = PlaySound(filename, 0, j)
  194.       SoundPlay = IIf(i = 0, False, True)
  195.    End If
  196. End Function
  197.  
  198. Public Function SoundStop(Optional audiotype As Variant)
  199.    If IsMissing(audiotype) Then
  200.       mciSendString "close filename", 0&, 0, 0
  201.       SoundPlay vbNullString, 0, 0
  202.    Else
  203.       If (audiotype And AUDIO_MIDI) = AUDIO_MIDI Then
  204.          mciSendString "close filename", 0&, 0, 0
  205.       End If
  206.       If (audiotype And AUDIO_WAVE) = AUDIO_WAVE Then
  207.          SoundPlay vbNullString, 0, 0
  208.       End If
  209.    End If
  210. End Function
  211.  
  212.